import {
    showModal,
    switchTab,
    showToast
  } from "../../utils/asyncWx.js";
import { remove, update } from "../../utils/cloud.js";
var id;
var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: '',
        //背景图片和输入颜色
        imageurl: '../../icons/common_bj.png',
        input_color:'white',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        id = e.id;
        
        getData(id, this);
    },
  
    /**
     * input change事件
     */
    change (e) {
        var { value } = e.detail;
        this.setData({
            content: value,
        });
    },

    /**
     * delete 事件
     */
    async delete () {
        const res = await showModal({ content: "您确定要删除吗？" });
        if (res.confirm) {
            // console.log("确定删除。");
            var diary = wx.getStorageSync('diary');
            //过滤掉要删除的，然后重新保存
            remove({
                table: "diary",
                id: id
            });
            diary = diary.filter((v) => v._id != id);
            wx.setStorageSync('diary', diary);
        } else {
            return;
        }
        this.show();
    },
    
    /**
     * 跳转页面
     */
    show () {
        showToast({ title: "跳转中", icon: 'loading' })
            .then(res => {
                var that = this;
                setTimeout(function () {
                    //存完清空输入框的内容
                    that.setData({
                        content: '',
                        imageurl: '../../icons/common_bj.png',
                        input_color: 'white',
                    });
                    app.data.type = "common";
                    switchTab({
                        url: "/pages/list/list"
                    });
                }, 1000);
                console.log(res);
            }).catch(err => {
                console.log(err);
            });
    },
    /**
     * 点击确认事件
     * @returns null
     */
    sure () {
        var reg = /^\s*$/g;
        if (!this.data.content || reg.test(this.data.content)) {
            console.log('修改不能置内容为空');
            return;
        }
        this.setData({
            time: Date.now()
        });
        setValue(this);
        this.show();
    }
});

/**
 * 根据跳转的url中的id获取编辑信息回填
 */
function getData(id, page) {
    var diary = wx.getStorageSync('diary');
    diary.forEach(item => {
        if (item._id == id) {
            page.setData({
                content: item.content,
                imageurl: item.type == 'mood' ? '../../icons/mood.png' : '../../icons/common_bj.png',
                input_color: item.type == 'mood' ? 'blue' : 'white',
            });
            return;
        }
    });
}

/**
 * 设置填写的信息，编辑
 */
function setValue (page) {
    var diary = wx.getStorageSync('diary');
    var up = [];
    diary.forEach(item => {
        if (item._id == id) {
            item.content = page.data.content;
            item.time = page.data.time;
            up.push(item);
        } else {
            up.push(item);
        }
    });
    //将修改的日记保存到本地里并上传到云端
    wx.setStorageSync("diary", up);
    update({
        table: "diary",
        id:id,
        content: page.data.content,
        time: page.data.time,
    });
}